Configuring Visual Studio Code to use PHP and PHP (Web) Server)
Open Visual Studio Code
Click on Extensions.
Search for php
Select PHP Server
Click Install
Install any other PHP packages Visual Studio recommends as well.
Install PHP Debug and PHP Intelephense as well to make editing code easier.
Testing it works
We need a php file.
Create a new file.
Save it as filename.php the filename can be anything. The extension .php will tell the webserver to run the php interpreter on this file.
In the top right of Visual Studio Code you should see a blue PHP Icon.
Now we want to add some code to our php file.
PHP files combine HTML and PHP script to create dynamic pages.
PHP is a programming language in its own right.
We use the tag **<?php**
to indicate the start of a block of php script for the server to run and a **?>**
tag to indicate where it will end.
Look at the example
<?php
?>
Enter the code below into your file.
echo will print to the html file. Here we are creating the date and printing 'Hello World'
<?php
echo date("d.m.y");
echo 'Hello World';
?>
Click the Blue PHP button to serve (run) the page.
We end up with a very bland webpage.
Modify your page so that it contains the code below.
This example integrates HTML and PHP.
Note that you can include HTML inside strings to print.
<html>
<body>
<h1>
<?php
echo date("d.m.y");
?>
</h1>
<?php
echo 'Hello <br> World';
?>
</body>
</html>
The server is already running at the moment. Clicking the Blue button will give an error.
Right click on the background of the php file and select Reload Server. You can also stop the server here.
Note that our page now has some html formatting
We now have a working webpage and a development PHP Web Server.